import { queryOrderById, cancelOrder } from '../../services/order'
import Toast from '@vant/weapp/toast/toast'
import { queryVehicleDetail } from '../../services/vehicle'
import { getPackageDetailInfo } from '../../services/inspect-project'
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderId:"",
    currentItem:{},
    appointTimeInfo:"",
    packageInfo:{},
    vehicleInfo:{},
    showDialogConfirm:false,
    dialogMsg:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }

    const orderId = options.orderId
    this.setData({
      orderId
    })
    this.getUserOrderInfo(orderId)
  },

  async getUserOrderInfo(id){
    let res = await queryOrderById(id)
    if(res.code === 200){
      let currentItem = res.data
      this.getPackageDetail(currentItem.appointPackageId)
      this.getVehicleDetail(currentItem.vehicleId)
      let appointTimeInfo = currentItem.appointDate + (currentItem.appointTime ? (currentItem.appointTime == "am" ? ' 上午': ' 下午'): '')
      this.setData({
        currentItem,
        appointTimeInfo
      })
    }
  },

  async getPackageDetail(packageId){
    let packageRes = await getPackageDetailInfo(packageId)
    if(packageRes.code === 200){
      let packageInfo = packageRes.data
      this.setData({
        packageInfo,
        ['currentItem.detectProjectName']:packageInfo.detectProjectName
      })
    }
  },

  async getVehicleDetail(vehicleId){
    let vehicleRes = await queryVehicleDetail(vehicleId)
    if(vehicleRes.code === 200){
      let vehicleInfo = vehicleRes.data
      this.setData({
        vehicleInfo,
        ['currentItem.vehicleType']:vehicleInfo.vehicleType
      })
    }
  },

  // async handleCancelOrder(){
  //   console.log("this.data.orderId>>>", this.data.orderId)
  //   let info = {
  //     id: this.data.orderId
  //   }
  //   let cancelRes = await cancelOrder(info)
  //   const _this = this
  //   if(cancelRes.code === 200){
  //     Toast.success('取消成功');
  //     setTimeout(function(){
  //       _this.handleBack()
  //     },1000)
  //   }
  // },

  handleBack(){
    wx.redirectTo({
      url: '/pages/order-list/order-list',
    })
  },

  handleEditOrder(event){
    let currentItem = event.detail
    const { id, appointDate, appointPackageId, appointPackageName, appointTime, detectProjectName, plateNo, stationId, stationName, vehicleId, vehicleType  } = currentItem

    let glabalInfo = {
      id,
      appointDate,
      appointPackageId,
      appointPackageName,
      appointTime,
      detectProjectName,
      plateNo,
      stationId,
      stationName,
      vehicleId,
      vehicleType
    }
    app.globalData.globalInfo = glabalInfo
    wx.navigateTo({
      url: '/pages/inspection-project/inspection-project',
    })
  },

  handleCancel(event){
    let currentItem = event.detail
    this.setData({
      showDialogConfirm: true,
      dialogMsg: "是否确认取消预约?",
      currentItem,
    })
  },

  async handleConfirm(){
    let info = {
      id: this.data.currentItem.id
    }
    const _this = this
    let cancelRes = await cancelOrder(info)
    if(cancelRes.code === 200){
      Toast.success('取消成功')
      // this.checkStatus(this.data.active)
      setTimeout(function(){
        _this.handleBack()
      },1000)
    }
  },

  handleViewReport(event){
    let orderId = event.detail?.id
    wx.navigateTo({
      url: `/pages/report-detail/report-detail?orderId=${orderId}`,
    })
  },

  async handleAppointmentAgain(event){
    let currentItem = event.detail
    const { appointPackageId, appointPackageName, plateNo, stationId, stationName, vehicleId  } = currentItem

    let vehicleRes = await queryVehicleDetail(vehicleId)
    let vehicleType = ""
    if(vehicleRes.code === 200){
      vehicleType = vehicleRes.data.vehicleType
    }

    let detectProjectName = ""
    let packageInfoRes = await getPackageDetailInfo(appointPackageId)
    if(packageInfoRes.code === 200){
      detectProjectName = packageInfoRes.data.detectProjectName
    }

    let glabalInfo = {
      appointPackageId,
      appointPackageName,
      detectProjectName,
      plateNo,
      stationId,
      stationName,
      vehicleId,
      vehicleType
    }
    app.globalData.globalInfo = glabalInfo
    wx.navigateTo({
      url: '/pages/inspection-project/inspection-project',
    })
  },

})